
/**
 * 尊敬的用户，你好：页面 JS 面板是高阶用法，一般不建议普通用户使用，如需使用，请确定你具备研发背景，能够自我排查问题。
 * 我们可以用 JS 面板来开发一些定制度高功能，比如：调用API接口，自定义动作执行等等。
 */

// 当页面渲染完毕后马上调用下面的函数，这个函数是在当前页面 - 设置 - 生命周期 - 页面加载完成时中被关联的。
export function didMount() {
    ("「页面 JS」：当前页面地址 " + location.href);
    const EVALUATE_STAT_CHART = 'csdn_detail_chart_line_80qmzz';
    const TASK_STAT_CHART = 'csdn_detail_chart_line_3fd422';
    const ENTERING_ENTERPRISE_STAT_CHART = 'csdn_form_chart_category_fya0bl';


    const self = this;
    const chartEle = self.$$(EVALUATE_STAT_CHART);
    const chartEle1 = self.$$(TASK_STAT_CHART);
    const chartEle2 = self.$$(ENTERING_ENTERPRISE_STAT_CHART);
    chartEle.data.props.loadingConfig.loading = true;
    chartEle1.data.props.loadingConfig.loading = true;
    chartEle2.data.props.loadingConfig.loading = true;
}


// 入企类型趋势
export function enteringEnterpriseStatChange() {
    const self = this;

    const ENTERING_ENTERPRISE_STAT_CHART = 'csdn_form_chart_category_fya0bl';
    const chartEle = self.$$(ENTERING_ENTERPRISE_STAT_CHART);


    const API_LIST = {
        'EVALUATE_STAT': "ds_api:a54d472a-4b3a-44e5-a5f3-e4f001835d9f", // api: 评价满意度趋势
        'TASK_STAT': "ds_api:5a051667-b436-4a65-aecd-1121ec872d47", // api: 涉企检查趋势
        'ENTERING_ENTERPRISE_STAT': "ds_api:91d501d7-b3d0-4951-b44f-fc681dc59ed9", // api: 入企类型趋势
    }

    let task_stat_type = self.$$("text_input_bipa7p").cValue;


    let typeParam = [{
        name: "timeRange",
        valueContent: task_stat_type
    }];

    if (task_stat_type === '-1') {
        typeParam.valueContent = null
    }
    fetchDdcatData(API_LIST.ENTERING_ENTERPRISE_STAT, typeParam).then(res => {
        debugger
        const result = res.result && res.result.length ? res.result.slice(0, 11).map((item) => {
            return {
                name: item.type_name,
                text: item.type_name,
                value: item.taskCount,
                unit: "次",
                code: item.code,
                checked: false
            }
        }) : [];

        chartEle.data.props.dataSource.options = result || [];
        chartEle.data.props.loadingConfig.loading = false;
    });

}


// 涉企检查趋势
export function taskStatOnChange(index) {

    const self = this;

    const TASK_STAT_CHART = 'csdn_detail_chart_line_3fd422';
    const chartEle = self.$$(TASK_STAT_CHART);


    const API_LIST = {
        'EVALUATE_STAT': "ds_api:a54d472a-4b3a-44e5-a5f3-e4f001835d9f", // api: 评价满意度趋势
        'TASK_STAT': "ds_api:5a051667-b436-4a65-aecd-1121ec872d47", // api: 涉企检查趋势
        'ENTERING_ENTERPRISE_STAT': 'ds_api:5b684e57292ea1c6c067793b3b744d03-2930', // api: 入企类型趋势
    }

    let task_stat_type = self.$$("text_input_1cqs45").cValue;

    const typeParam = [{
        name: "type",
        valueContent: task_stat_type
    }];



    // 联合情况
    fetchDdcatData(API_LIST.TASK_STAT, typeParam).then(res => {

        const transformedData = [
            {
                name: 'jccs',
                text: '检查次数',
                value: res.result.map(item => ({
                    text: item.check_time,
                    value: item.taskCount
                })),

                checked: false
            }
        ];
        chartEle.data.props.dataSource.options = transformedData || [];
        chartEle.data.props.loadingConfig.loading = false;
    });
}


// 评价满意度趋势
export function evaluateStatOnChange() {
    const self = this;

    const EVALUATE_STAT_CHART = 'csdn_detail_chart_line_80qmzz';
    const chartEle = self.$$(EVALUATE_STAT_CHART);


    const API_LIST = {
        'EVALUATE_STAT': "ds_api:a54d472a-4b3a-44e5-a5f3-e4f001835d9f", // api: 评价满意度趋势
        'TASK_STAT': 'ds_api:09a749c27100e68db9c83da792e9fa47-2930', // api: 涉企检查趋势
        'ENTERING_ENTERPRISE_STAT': 'ds_api:5b684e57292ea1c6c067793b3b744d03-2930', // api: 入企类型趋势
    }

    let task_stat_type = self.$$("text_input_81vthw").cValue;

    const typeParam = [{
        name: "type",
        valueContent: task_stat_type
    }];



    // 评价满意度趋势
    fetchDdcatData(API_LIST.EVALUATE_STAT, typeParam).then(res => {

        const transformedData = [
            {
                name: 'my',
                text: '满意',
                value: res.result.map(item => ({
                    text: item.create_time,
                    value: item.myCount
                })),
                checked: false
            }, {
                name: 'bmy',
                text: '不满意',
                value: res.result.map(item => ({
                    text: item.create_time,
                    value: item.bmyCount
                })),
                checked: false
            }
        ];
        chartEle.data.props.dataSource.options = transformedData || [];
        chartEle.data.props.loadingConfig.loading = false;
    });
}




function fetchDdcatData(sourceId, params) {
    const jsonParams = { pageSize: 99999 };
    return api.DATA_SOURCE_EXECUTE(
        sourceId,
        "api",
        params,
        [],
        JSON.stringify(jsonParams)
    ).then(res => {

        if (res && !res.hasError && res.result != null) {
            return JSON.parse(res.result);
        } else {
            console.warn("查询失败或数据为空，生成模拟数据");
            return [];
        }
    });
}
